package com.yxzx.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxzx.api.dto.CourseReq;
import com.yxzx.api.dto.RestStatusCode;
import com.yxzx.api.entity.*;
import com.yxzx.api.exception.CustomException;
import com.yxzx.api.service.ICategoryService;
import com.yxzx.api.service.ICourseOrderService;
import com.yxzx.api.service.ICourseService;
import com.yxzx.api.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author YWB
 * @since 2020-09-06
 */
@RestController
@RequestMapping("/api/course")
public class CourseController extends BaseController{

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ICourseService courseService;



    //查询课程列表
    @PostMapping("/getCourseList")
    @ApiOperation("课程列表查询")
    public ResponseEntity<Page<Course>> getCourseList(@RequestBody @ApiParam("课程查询条件") CourseReq courseReq){

        //1.分析前端传来的条件
        //如果为空，初始化courseReq
        if(courseReq==null){
            courseReq=new CourseReq();
        }

        try{
            //拼接查询条件
            QueryWrapper<Course> queryWrapper=new QueryWrapper<>();

            //列表页前端需要字段
            queryWrapper.select("id","title","image","sale_count","price");

            //判断类别是一级分类还是二级科目
            Category category = null;
            if(courseReq.getCategoryId()!=null)
                category=categoryService.getById(courseReq.getCategoryId());

            if(category!=null){

                if(category.getLevel()==1){
                    courseReq.setSubjectId(0);
                }else {
                    courseReq.setSubjectId(category.getId());
                    courseReq.setCategoryId(category.getId());
                }

            }else {
                courseReq.setCategoryId(0);
                courseReq.setSubjectId(0);
            }

            //进行查询
            Course course=new Course();
            course.setCategoryId(courseReq.getCategoryId()==null || courseReq.getCategoryId()==0 ? null : courseReq.getCategoryId());
            course.setSubjectId(courseReq.getSubjectId()==null || courseReq.getSubjectId()==0 ? null : courseReq.getSubjectId());
            course.setTeachType(courseReq.getTeachType()==null || courseReq.getTeachType()==0 ? null : courseReq.getTeachType());
            course.setPriceType(courseReq.getPriceType()==null || courseReq.getPriceType()==0 ? null : courseReq.getPriceType());

            queryWrapper.setEntity(course);
            //排序条件
            if(courseReq.getSortType()!=null){
                switch (courseReq.getSortType()){
                    case 1:
                        queryWrapper.orderByDesc("sale_count");//销量倒序
                        break;
                    case 2:
                        queryWrapper.orderByDesc("create_time");
                        break;
                    case 3:
                        queryWrapper.orderByDesc("price");//价格倒序
                        break;
                    case 4:
                        queryWrapper.orderByAsc("price");//价格升序
                        break;
                }
            }

            //价格区间
            if(courseReq.getStartPrice()!=null && courseReq.getStartPrice()>0){
                //起始价
                queryWrapper.ge("price",courseReq.getStartPrice());
            }

            if(courseReq.getEndPrice()!=null && courseReq.getEndPrice()>courseReq.getStartPrice()){
                //结束价
                queryWrapper.le("price",courseReq.getEndPrice());
            }

            //分页查询
            Page<Course> page=new Page<>(courseReq.getPageNo(),courseReq.getPageSize());
            page.setSearchCount(true);

            page = courseService.page(page, queryWrapper);

            return ResponseEntity.ok(page);
        }catch(Exception e){
            e.printStackTrace();
            throw new CustomException("", RestStatusCode.COURSE_QUERY_LIST_ERROR.getCode(),RestStatusCode.COURSE_QUERY_LIST_ERROR.getMessage());
        }
    }

    @GetMapping("/{courseId}")
    @ApiOperation("获取课程详情")
    public ResponseEntity getCourse(@PathVariable(value ="courseId",required = true) @ApiParam("课程ID")  Long courseId ){


        try{

            Course course = courseService.getById(courseId);

            //判断是否有此课
            if(course!=null){
                //检测课有没有购买
                course.setBought(isBuyCourse(courseId,getUser()));
                //所属类别,科目
                Category category=categoryService.getById(course.getCategoryId());
                Category subject=categoryService.getById(course.getSubjectId());

                course.setCategoryName(category.getName());
                course.setSubjectName(subject.getName());


                return ResponseEntity.ok(course);
            }else {

                throw new CustomException(courseId+"", RestStatusCode.COURSE_QUERY_ERROR.getCode(),RestStatusCode.COURSE_QUERY_ERROR.getMessage());
            }
        }catch(CustomException e){

            e.printStackTrace();
            throw new CustomException(courseId+"",RestStatusCode.COURSE_QUERY_ERROR.getCode(),RestStatusCode.COURSE_QUERY_ERROR.getMessage());
        }



    }





}
